"""
进度 | 映射
"""

from mapping.base_mapping import (
    MappingBase,
    get_field_map,
)
from models.other import ProcessHistory
from models.po import ProcessPO
from models.vo import (
    ProcessDetailVO,
    ProcessHistoryVO,
    ProcessListPage,
    ProcessListVO,
)
from utils.converter_utils import TimeConverter


def _parse_history(history: list[ProcessHistory]) -> list[ProcessHistoryVO]:
    """ 解析历史记录 """

    return [
        ProcessHistoryVO(
            percent=i.percent,
            update_time=TimeConverter.datetime_2_datetime_full_str(i.update_time),
        )
        for i in history
    ]


class ProcessMapping(MappingBase):
    """ 进度 | 映射 """

    id_field = "process_id"
    po = ProcessPO
    list_vo = ProcessListVO
    detail_vo = ProcessDetailVO
    vo_page = ProcessListPage

    list_mapping = {
        "status_label": get_field_map(["status", "label"]),
        "task_label": get_field_map(["task", "label"]),
    }

    detail_mapping = {
        "history": get_field_map(["history"], func=_parse_history),
    }
